/* TCSS 590A - Cybernetics and Systems
 * Group Project - Modeling
 * By Barry Jones, Naren Meadem, Michael Pitts, Yuan Tao
 * Fall 2012, University of Washington, Tacoma
 */
package model.delayed;


/**
 * An account that will not accrue interest while positive, but WILL go more 
 * negative when value is less than zero.
 * @author Michael Pitts
 * @version Nov 26, 2012
 */
public class DebtableAccount extends BasicAccount
{
	/** Minimum value this DebtableAccount can take.*/
	private final long my_min;
	
	/**
	 * Creates a DebtableAccount where there may be some delay when making a 
	 * withdrawal.
	 * @param the_value is the starting value of the Account.
	 * @param the_rate is the rate debt grows when the value is negative.
	 * @param the_generator is what determines the delay on withdrawal.
	 * @param the_min is the minimum value this DebtableAccount can take before it
	 * is in an illegal state.
	 */
	public DebtableAccount(final long the_value, final double the_rate,
			final Generator the_generator, final long the_min)
	{
		super(the_value, the_rate, the_generator);
		my_min = the_min;
	}

	/**
	 * Creates a new DebtableAccount where there is no delay when withdrawing.
	 * @param the_value is the starting value of the Account.
	 * @param the_rate is the rate debt grows when the value is negative.
	 * @param the_min is the minimum value this DebtableAccount can take before it
	 * is in an illegal state.
	 */
	public DebtableAccount(final long the_value, final double the_rate,
			final long the_min)
	{
		this(the_value, the_rate, null, the_min);
	}

	/**
	 * Creates a new DebtableAccount where having a negative value has no effect.
	 * @param the_value is the starting value of this Account.
	 * @param the_min is the minimum value this DebtableAccount can take before it
	 * is in an illegal state.
	 */
	public DebtableAccount(final long the_value, final long the_min)
	{
		this(the_value, 0, null, the_min);
	}

	@Override public void addInterest()
	{
		if (getValue() < 0)
		{
			final double interest = getRate() * (getValue() + getFraction());
			changeValue((long) Math.ceil(interest));
			changeFraction(interest - Math.ceil(interest));
			if (getFraction() <= -1) {
				changeValue((long) Math.ceil(getFraction()));
				changeFraction(-Math.ceil(getFraction()));
			}
		}
	}
	
	@Override public boolean inLegalState()
	{
		return getValue() >= my_min;
	}
}
